<?php
class Core_Model_Session {

	protected $_namespace = 'default';
	
	public function __construct() {
		if ($this->_getResponse()->canSendHeaders(true)) {
			session_start();
		}
	}

	public function get($name=null) {
		$data = $this->_getNamespaceData();
		if ($name === null)
			return $data;
		return (isset($data[$name])) ? $data[$name] : null;
	}
	
	public function set($name, $value) {
		$data = $this->_getNamespaceData();
		if ($value===null)
			unset($data[$name]);
		else
			$data[$name] = $value;
		return $this;
	}
	
	public function clear($all=false) {
		if ($all) {
			session_destroy();
		} else {
			unset($_SESSION[$this->_namespace]);
		}
		return $this;
	}
	
	protected function _getResponse() {
		return Mag::getResponse();
	}

	protected function &_getNamespaceData() {
		if (!isset($_SESSION[$this->_namespace]))
			$_SESSION[$this->_namespace] = array();
		return $_SESSION[$this->_namespace];
	}
}